package com.ssyt.ec.que.service;

import java.util.Date;

import com.ssyt.ec.common.PageModel;
import com.ssyt.ec.common.util.DBUtil;
import com.ssyt.ec.que.entity.QuestionEntity;

/**
 * 试题
 * 
 * @author Administrator
 *
 */
public class QestionsService {

	/**
	 * 试题的模糊查询
	 * 
	 * @param name
	 * @return
	 */
	public PageModel<QuestionEntity> selectQes(String question, int page,
			int rows) {
		return DBUtil
				.executeQueryByPage(
						QuestionEntity.class,
						"SELECT q.id,q.question,q.attachment,q.question_type,q.course_id,q.answer_a,q.answer_b,q.answer_c,q.answer_d,q.answer,q.difficulty,q.operate_user_id,q.operate_time,q.state,s.name,u.name as username FROM qes_qestions q,sys_course s,sys_user u WHERE q.course_id=s.id AND q.operate_user_id=u.id AND q.state=1 AND Q.question LIKE ? ORDER BY q.id DESC LIMIT ?, ?",
						"%" + question + "%", (page - 1) * rows, rows);
	}

	/**
	 * 试题的条件查询（科目）
	 * 
	 * @param name
	 * @return
	 */
	public PageModel<QuestionEntity> selectCousre(int Cousre, int page, int rows) {
		return DBUtil
				.executeQueryByPage(
						QuestionEntity.class,
						"SELECT q.id,q.question,q.attachment,q.question_type,q.course_id,q.answer_a,q.answer_b,q.answer_c,q.answer_d,q.answer,q.difficulty,q.operate_user_id,q.operate_time,q.state,s.name,u.name as username FROM qes_qestions q,sys_course s,sys_user u WHERE q.course_id=s.id AND q.operate_user_id=u.id AND q.state=1 AND s.id=? ORDER BY q.id DESC LIMIT ?, ?",
						Cousre, (page - 1) * rows, rows);
	}

	/**
	 * 试题的条件查询（题型）
	 * 
	 * @param name
	 * @return
	 */
	public PageModel<QuestionEntity> selectType(int type, int page, int rows) {
		return DBUtil
				.executeQueryByPage(
						QuestionEntity.class,
						"SELECT q.id,q.question,q.attachment,q.question_type,q.course_id,q.answer_a,q.answer_b,q.answer_c,q.answer_d,q.answer,q.difficulty,q.operate_user_id,q.operate_time,q.state,s.name,u.name as username FROM qes_qestions q,sys_course s,sys_user u WHERE q.course_id=s.id AND q.operate_user_id=u.id AND q.state=1 AND q.question_type=? ORDER BY q.id DESC LIMIT ?, ?",
						type, (page - 1) * rows, rows);
	}

	/**
	 * 试题的条件查询（难度）
	 * 
	 * @param name
	 * @return
	 */
	public PageModel<QuestionEntity> selectDif(int dif, int page, int rows) {
		return DBUtil
				.executeQueryByPage(
						QuestionEntity.class,
						"SELECT q.id,q.question,q.attachment,q.question_type,q.course_id,q.answer_a,q.answer_b,q.answer_c,q.answer_d,q.answer,q.difficulty,q.operate_user_id,q.operate_time,q.state,s.name,u.name as username FROM qes_qestions q,sys_course s,sys_user u WHERE q.course_id=s.id AND q.operate_user_id=u.id AND q.state=1 AND q.difficulty=? ORDER BY q.id DESC LIMIT ?, ?",
						dif, (page - 1) * rows, rows);
	}

	/**
	 * 修改状态
	 * 
	 * @param id
	 * @param value
	 * @return
	 */
	public int updateStateQue(int id, int value) {
		return DBUtil.executeUpdate(
				"UPDATE qes_qestions SET state=? WHERE id=?", value, id);
	}

	/**
	 * 修改试题
	 * 
	 * @param id
	 * @param menuName
	 * @param url
	 * @param state
	 * @return
	 */
	public int updateQue(int id, String question, String answer_a,
			String answer_b, String answer_c, String answer_d, String answer,
			int uestion_type, int difficulty) {
		return DBUtil
				.executeUpdate(
						"UPDATE qes_qestions SET question=?,question_type=?,answer_a=?,answer_b=?,answer_c=?,answer_d=?,answer=?,difficulty=? WHERE id=?",
						question, uestion_type, answer_a, answer_b, answer_c,
						answer_d, answer, difficulty, id);
	}

	/**
	 * 显示有效试题
	 * 
	 * @return
	 */
	public PageModel<QuestionEntity> listQuestioinsOp(int page, int rows) {
		return DBUtil
				.executeQueryByPage(
						QuestionEntity.class,
						"SELECT q.id,q.question,q.attachment,q.question_type,q.course_id,q.answer_a,q.answer_b,q.answer_c,q.answer_d,q.answer,q.difficulty,q.operate_user_id,q.operate_time,q.state,s.name,u.name as username FROM qes_qestions q,sys_course s,sys_user u WHERE q.course_id=s.id AND q.operate_user_id=u.id AND q.state=1 ORDER BY q.id DESC LIMIT ?, ?",
						page, rows);
	}

	/**
	 * 显示无效试题
	 * 
	 * @return
	 */
	public PageModel<QuestionEntity> listQuestioinsClo(int page, int rows) {
		return DBUtil
				.executeQueryByPage(
						QuestionEntity.class,
						"SELECT q.id,q.question,q.attachment,q.question_type,q.course_id,q.answer_a,q.answer_b,q.answer_c,q.answer_d,q.answer,q.difficulty,q.operate_user_id,q.operate_time,q.state,s.name,u.name as username FROM qes_qestions q,sys_course s,sys_user u WHERE q.course_id=s.id AND q.operate_user_id=u.id AND q.state=2 ORDER BY q.id DESC LIMIT ?, ?",
						page, rows);
	}

	/**
	 * 试题的新增
	 * 
	 * @param course
	 * @param type
	 * @param difficulty
	 * @param question
	 * @param a
	 * @param b
	 * @param c
	 * @param d
	 * @param answer
	 * @return
	 */
	public int addQes(String course, int type, int difficulty, String question,
			String a, String b, String c, String d, String answer) {
		Date date = new Date();
		return DBUtil
				.executeUpdate(
						"INSERT INTO qes_qestions VALUES (NULL,?,NULL,?,?,?,?,?,?,?,?,NULL,NULL,1,?,1,NULL)",
						question, type, course, a, b, c, d, answer, difficulty,
						date);
	}
}
